package com.xr.core.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.xr.core.system.common.base.MyServiceImpl;
import com.xr.core.system.common.exception.ServiceException;
import com.xr.core.system.common.utils.CurrentLoginUserUtil;
import com.xr.core.system.entity.permissionModule.Permission;
import com.xr.core.system.mapper.PermissionMapper;
import com.xr.core.system.service.PermissionService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 总后台 权限表(Permission)表服务实现类
 */
@Service("permissionService")
public class PermissionServiceImpl extends MyServiceImpl<PermissionMapper, Permission> implements PermissionService {

    /**
     * 获取整个权限树
     *
     * @return
     */
    @Override
    public List<Permission> getPermissionTree() {
        LambdaQueryWrapper<Permission> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Permission::getEnable, true).orderByAsc(Permission::getSort);
        List<Permission> permissions = baseMapper.selectList(queryWrapper);
        Map<Long, List<Permission>> collect = permissions.stream().collect(Collectors.groupingBy(Permission::getParentId));
        List<Permission> parent = collect.get(0L);
        parseList(parent, collect);
        return parent;
    }

    /**
     * 递归拼接菜单
     */
    private void parseList(List<Permission> parent, Map<Long, List<Permission>> permissions) {
        for (Permission permission : parent) {
            Long id = permission.getId();
            List<Permission> children = permissions.get(id);
            if (CollectionUtil.isNotEmpty(children)) {
                permission.setPChild(children);
                parseList(children, permissions);
            }
        }
    }

    /**
     * 新增数据
     *
     * @param permission 实体对象
     * @return 新增结果
     */
    @Override
    public Boolean insert(Permission permission) {
        LambdaQueryWrapper<Permission> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Permission::getCode, permission.getCode());
        Permission exist = baseMapper.selectOne(queryWrapper);
        if (exist != null) {
            throw new ServiceException("当前权限code已存在！");
        }
        permission.setEnable(true);
        permission.setInitial(false);
        return SqlHelper.retBool(baseMapper.insert(permission));
    }

    /**
     * 修改数据
     *
     * @param permission 实体对象
     * @return 修改结果
     */
    @Override
    public boolean update(Permission permission) {
        if (StrUtil.isNotBlank(permission.getCode())) {
            LambdaQueryWrapper<Permission> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(Permission::getCode, permission.getCode()).ne(Permission::getId, permission.getId());
            Permission exist = baseMapper.selectOne(queryWrapper);
            if (exist != null) {
                throw new ServiceException("当前权限code已存在!");
            }
        }
        return SqlHelper.retBool(baseMapper.updateById(permission));
    }

    /**
     * 获取当前登录用户的权限url
     *
     * @return
     */
    @Override
    public List<String> getAllPermissionUrlByUserId() {
        return baseMapper.getAllPermissionUrlByUserId(CurrentLoginUserUtil.getUserId());
    }

    /**
     * 获取当前登录人的按钮权限信息
     *
     * @return
     */
    @Override
    public List<String> getAllBtnPermissionByUserId() {
        return baseMapper.getAllBtnPermissionByUserId(CurrentLoginUserUtil.getUserId());
    }

}

